//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CurveState.h"
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (boost::shared_ptr<QuantLib::CurveState>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCurveState = NULL;
#endif
	_ppCurveState = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (QuantLib::CurveState& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCurveState = NULL;
#endif
	_ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (&childNative);
    
    _CurveStateOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (CCurveState^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCurveState = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCurveState = NULL;
#endif
	if (!t->IsSubclassOf(CCurveState::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (QuantLib::Handle<QuantLib::CurveState>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCurveState = &childNative;
	_ppCurveState = &static_cast<boost::shared_ptr<QuantLib::CurveState>>(childNative.currentLink());
    
    _CurveStateOwner = owner;
}
Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (QuantLib::Handle<QuantLib::CurveState> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCurveState = &childNative;
	_ppCurveState = &static_cast<boost::shared_ptr<QuantLib::CurveState>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CCurveState::CCurveState (QuantLib::CurveState childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCurveState = NULL;
#endif
	_ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (new QuantLib::CurveState (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CCurveState::~CCurveState ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCurveState != NULL)
    {
	    delete _ppCurveState;
        _ppCurveState = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CCurveState::!CCurveState ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCurveState != NULL)
    {
	    delete _ppCurveState;
    }
}
QuantLib::CurveState& Cephei::QL::Models::Marketmodels::CCurveState::GetReference ()
{
    if (_ppCurveState == NULL) throw REFNEW NativeNullException ();
	return **_ppCurveState;
}
boost::shared_ptr<QuantLib::CurveState>& Cephei::QL::Models::Marketmodels::CCurveState::GetShared ()
{
    if (_ppCurveState == NULL) throw REFNEW NativeNullException ();
	return *_ppCurveState;
}
QuantLib::CurveState* Cephei::QL::Models::Marketmodels::CCurveState::GetPointer ()
{
    if (_ppCurveState == NULL) throw REFNEW NativeNullException ();
	return &**_ppCurveState;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CurveState>& Cephei::QL::Models::Marketmodels::CCurveState::GetHandle ()
{
	if (_phCurveState == NULL)
	{
		_phCurveState = new Handle<QuantLib::CurveState> (*_ppCurveState);
	}
	return *_phCurveState;
}
#endif
bool Cephei::QL::Models::Marketmodels::CCurveState::HasNative () 
{
	return (_ppCurveState != NULL);
}

Double Cephei::QL::Models::Marketmodels::CCurveState::CmSwapAnnuity (UInt64 numeraire, UInt64 i, UInt64 spanningForwards)
{
    try
    {
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //a
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _spanningForwards = (QuantLib::Size)ValueHelper::Convert (spanningForwards); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCurveState)->cmSwapAnnuity ( _numeraire,  _i,  _spanningForwards );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CCurveState::CmSwapRate (UInt64 i, UInt64 spanningForwards)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _spanningForwards = (QuantLib::Size)ValueHelper::Convert (spanningForwards); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCurveState)->cmSwapRate ( _i,  _spanningForwards );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CCurveState::CmSwapRates (UInt64 spanningForwards)
{
    try
    {
        QuantLib::Size _spanningForwards = (QuantLib::Size)ValueHelper::Convert (spanningForwards); //a
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCurveState)->cmSwapRates ( _spanningForwards );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CCurveState::CoterminalSwapAnnuity (UInt64 numeraire, UInt64 i)
{
    try
    {
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //a
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCurveState)->coterminalSwapAnnuity ( _numeraire,  _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CCurveState::CoterminalSwapRate (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCurveState)->coterminalSwapRate ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CCurveState::CoterminalSwapRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCurveState)->coterminalSwapRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CCurveState::DiscountRatio (UInt64 i, UInt64 j)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCurveState)->discountRatio ( _i,  _j );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CCurveState::ForwardRate (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCurveState)->forwardRate ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CCurveState::ForwardRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCurveState)->forwardRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CCurveState::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCurveState)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CCurveState::RateTaus::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppCurveState)->rateTaus ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CCurveState::RateTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppCurveState)->rateTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CCurveState::SwapRate (UInt64 begin, UInt64 end)
{
    try
    {
        QuantLib::Size _begin = (QuantLib::Size)ValueHelper::Convert (begin); //a
        QuantLib::Size _end = (QuantLib::Size)ValueHelper::Convert (end); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCurveState)->swapRate ( _begin,  _end );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

